package com.bw.zg3park2212.merchant

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.MerchantEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.MerchantAdpter
import com.bw.zg3park2212.databinding.ActivityMerchantBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/MerchantActivity")
class MerchantActivity : BaseActivity<ActivityMerchantBinding,MerchantViewModel>() {
    lateinit var merchantAdpter: MerchantAdpter
    var list = mutableListOf<MerchantEntityItem>()
    override fun intitView() {
        merchantAdpter= MerchantAdpter()
        binding.rvMerchant.adapter=merchantAdpter
        binding.rvMerchant.layoutManager=LinearLayoutManager(this)
        binding.merchantRvstatus.setOnCheckedChangeListener { group, checkedId ->
            merchantAdpter.data.clear()
            if (checkedId==R.id.merchant_a){
                merchantAdpter.addData(list.filter { it.status =="新建"})
            }else{
                merchantAdpter.addData(list.filter { it.status !="新建"})
            }
            merchantAdpter.notifyDataSetChanged()
        }
        merchantAdpter.addChildClickViewIds(R.id.merchant_status)
        merchantAdpter.setOnItemChildClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/MerchantXiangActivity")
                .withSerializable("mmm",merchantAdpter.data[position])
                .navigation()
        }
        merchantAdpter.setOnItemLongClickListener { adapter, view, position ->
            merchantAdpter.remove(position)
            viewmodel.getmerchandel(merchantAdpter.data[position].id)
            return@setOnItemLongClickListener true
        }
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/MerchantAddActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.merchanSuccse.observe(this){
            list.clear()
            merchantAdpter.data.clear()
            list.addAll(it)
            merchantAdpter.addData(list.filter { it.status =="新建"})
            merchantAdpter.notifyDataSetChanged()
        }
        viewmodel.merchandelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getmerchan()
        binding.merchantRvstatus.check(R.id.merchant_a)
    }
}